package hoowin.basic.service;

import java.security.NoSuchAlgorithmException;

import hoowin.basic.dao.IAccountDao;
import hoowin.basic.model.Account;
import hoowin.basic.model.User;
import hoowin.basic.util.Encoder;
import hoowin.basic.util.Validator;

public abstract class LogService {
	private IAccountDao accountDao;
	
	public String doLogin(String name, String origin_password){
		Account account;
		String password = null;
		try {
			password = Encoder.encryptMD5(origin_password);
		} catch (NoSuchAlgorithmException e) {
			return "exception.1000";
		}
		if(Validator.isPhoneNumber(name, true))account = accountDao.getAccountByPhone(name);
		else if(Validator.isEmailAddress(name, true))account =accountDao.getAccountByEmail(name);
		else return "error.1000";
		if(account==null||account.getId()==0)return "error.1001";
		else{
			if(account.getPassword().equals(password)){
				User user = getUser();
				if(user.getId()>0)user.destroy();
				user.login(user.getUp().getUserById(account.getId()), account);
				return "success";
			}else return "error.1002";
		}
	}
	
	public void setAccountDao(IAccountDao accountDao) {
		this.accountDao = accountDao;
	}
	public abstract User getUser();
}
